package com.common.client.map;

import java.util.Set;
import java.util.HashMap;

import org.gwtopenmaps.openlayers.client.LonLat;
import org.gwtopenmaps.openlayers.client.Map;
import org.gwtopenmaps.openlayers.client.Pixel;
import org.gwtopenmaps.openlayers.client.event.EventHandler;
import org.gwtopenmaps.openlayers.client.event.EventObject;
import org.gwtopenmaps.openlayers.client.event.EventType;
import org.gwtopenmaps.openlayers.client.util.JSObject;

public class GisMapEvent {

	private static final HashMap<Integer, String> mapGisTypesToEventTypes = new HashMap<Integer, String>();
	private static final HashMap<String, Integer> mapEventTypesToGisTypes = new HashMap<String, Integer>();

	public static final int CONTROL_ACTIVATE = 1;
	public static final int CONTROL_DEACTIVATE = 2;
	public static final int CONTROL_MEASURE = 3;
	public static final int CONTROL_MEASURE_PARTIAL = 4;
	public static final int CONTROL_GET_FEATURE_INFO = 5;
	public static final int CONTROL_GET_FEATURE_INFO_BEFORE = 6;
	public static final int CONTROL_SELECT_FEATURE_SELECT = 7;
	public static final int CONTROL_SELECT_FEATURE_UNSELECT = 8;
	public static final int CONTROL_SELECT_FEATURE_BEFORE_HIGHLIGHTED = 9;
	public static final int CONTROL_SELECT_FEATURE_HIGHLIGHTED = 10;
	public static final int CONTROL_SELECT_FEATURE_UNHIGHLIGHTED = 11;

	public static final int LAYER_LOADSTART = 12;
	public static final int LAYER_LOADEND = 13;
	public static final int LAYER_LOADCANCEL = 14;
	public static final int LAYER_VISIBILITYCHANGED = 15;

	public static final int MAP_BASE_LAYER_CHANGED = 16;
	public static final int MAP_CLICK = 17;
	public static final int MAP_LAYER_ADDED = 18;
	public static final int MAP_LAYER_CHANGED = 19;
	public static final int MAP_LAYER_REMOVED = 20;
	public static final int MAP_MOVE = 21;
	public static final int MAP_MOVEEND = 22;
	public static final int MAP_ZOOMEND = 23;
	public static final int MAP_POPUP_OPEN = 24;
	public static final int MAP_POPUP_CLOSE = 25;
	public static final int MAP_MARKER_ADDED = 26;
	public static final int MAP_MARKER_REMOVED = 27;

	public static final int VECTOR_BEFORE_FEATURE_ADDED = 28;
	public static final int VECTOR_FEATURE_ADDED = 29;
	public static final int VECTOR_FEATURE_MODIFIED = 30;
	public static final int VECTOR_FEATURE_REMOVED = 31;
	public static final int VECTOR_FEATURE_SELECTED = 32;
	public static final int VECTOR_FEATURE_UNSELECTED = 33;

	public static final int MAP_MOUSEUP = 34;
	public static final int MAP_MOUSEDOWN = 35;

	private int event_type;
	private String event_name;
	private Map map;
	private LonLat mousePos;
	private int clientX;
	private int clientY;
	private Pixel xy;
	private EventObject eventObject;
	private JSObject obj;
	private int button;

	public GisMapEvent(int event_type, String event_name, Map map,
			LonLat mousePos, int clientX, int clientY, Pixel xy,
			EventObject eventObject, JSObject obj, int button) {
		this.event_type = event_type;
		this.event_name = event_name;
		this.map = map;
		this.mousePos = mousePos;
		this.clientX = clientX;
		this.clientY = clientY;
		this.xy = xy;
		this.eventObject = eventObject;
		this.obj = obj;
		this.button = button;
	}

	public GisMapEvent(EventObject eventObject, Map map) {
		this.eventObject = eventObject;
		event_name = eventObject.getType();
		event_type = mapEventTypesToGisTypes.get(event_name);

		JSObject obj = eventObject.getJSObject();
		try {
			int button = obj.getPropertyAsInt("button");
			this.button = button;
			Pixel xy = Pixel.narrowToPixel(obj.getProperty("xy"));
			int clientX = obj.getPropertyAsInt("clientX");
			int clientY = obj.getPropertyAsInt("clientY");
			this.clientX = clientX;
			this.clientY = clientY;
			this.xy = xy;
			mousePos = map.getLonLatFromPixel(xy);
		} catch (Exception e) {
			// TODO: handle exception
		}
		this.map = map;

		this.eventObject = eventObject;
		this.obj = obj;
	}

	public int getEvent_type() {
		return event_type;
	}

	public String getEvent_name() {
		return event_name;
	}

	public Map getMap() {
		return map;
	}

	public LonLat getMousePos() {
		return mousePos;
	}

	public int getClientX() {
		return clientX;
	}

	public int getClientY() {
		return clientY;
	}

	public Pixel getXy() {
		return xy;
	}

	public EventObject getEventObject() {
		return eventObject;
	}

	public JSObject getObj() {
		return obj;
	}

	public int getButton() {
		return button;
	}

	public static void registerAllEvents(EventHandler eh, Map map) {
		Set<String> keys = mapEventTypesToGisTypes.keySet();
		for (String key : keys) {
			map.getEvents().register(key, map, eh);
		}
	}

	static {
		mapGisTypesToEventTypes.put(CONTROL_ACTIVATE,
				EventType.CONTROL_ACTIVATE);
		mapEventTypesToGisTypes.put(EventType.CONTROL_ACTIVATE,
				CONTROL_ACTIVATE);
		mapGisTypesToEventTypes.put(CONTROL_DEACTIVATE,
				EventType.CONTROL_DEACTIVATE);
		mapEventTypesToGisTypes.put(EventType.CONTROL_DEACTIVATE,
				CONTROL_DEACTIVATE);
		mapGisTypesToEventTypes.put(CONTROL_MEASURE, EventType.CONTROL_MEASURE);
		mapEventTypesToGisTypes.put(EventType.CONTROL_MEASURE, CONTROL_MEASURE);
		mapGisTypesToEventTypes.put(CONTROL_MEASURE_PARTIAL,
				EventType.CONTROL_MEASURE_PARTIAL);
		mapEventTypesToGisTypes.put(EventType.CONTROL_MEASURE_PARTIAL,
				CONTROL_MEASURE_PARTIAL);
		mapGisTypesToEventTypes.put(CONTROL_GET_FEATURE_INFO,
				EventType.CONTROL_GET_FEATURE_INFO);
		mapEventTypesToGisTypes.put(EventType.CONTROL_GET_FEATURE_INFO,
				CONTROL_GET_FEATURE_INFO);
		mapGisTypesToEventTypes.put(CONTROL_GET_FEATURE_INFO_BEFORE,
				EventType.CONTROL_GET_FEATURE_INFO_BEFORE);
		mapEventTypesToGisTypes.put(EventType.CONTROL_GET_FEATURE_INFO_BEFORE,
				CONTROL_GET_FEATURE_INFO_BEFORE);
		mapGisTypesToEventTypes.put(CONTROL_SELECT_FEATURE_SELECT,
				EventType.CONTROL_SELECT_FEATURE_SELECT);
		mapEventTypesToGisTypes.put(EventType.CONTROL_SELECT_FEATURE_SELECT,
				CONTROL_SELECT_FEATURE_SELECT);
		mapGisTypesToEventTypes.put(CONTROL_SELECT_FEATURE_UNSELECT,
				EventType.CONTROL_SELECT_FEATURE_UNSELECT);
		mapEventTypesToGisTypes.put(EventType.CONTROL_SELECT_FEATURE_UNSELECT,
				CONTROL_SELECT_FEATURE_UNSELECT);
		mapGisTypesToEventTypes.put(CONTROL_SELECT_FEATURE_BEFORE_HIGHLIGHTED,
				EventType.CONTROL_SELECT_FEATURE_BEFORE_HIGHLIGHTED);
		mapEventTypesToGisTypes.put(
				EventType.CONTROL_SELECT_FEATURE_BEFORE_HIGHLIGHTED,
				CONTROL_SELECT_FEATURE_BEFORE_HIGHLIGHTED);
		mapGisTypesToEventTypes.put(CONTROL_SELECT_FEATURE_HIGHLIGHTED,
				EventType.CONTROL_SELECT_FEATURE_HIGHLIGHTED);
		mapEventTypesToGisTypes.put(
				EventType.CONTROL_SELECT_FEATURE_HIGHLIGHTED,
				CONTROL_SELECT_FEATURE_HIGHLIGHTED);
		mapGisTypesToEventTypes.put(CONTROL_SELECT_FEATURE_UNHIGHLIGHTED,
				EventType.CONTROL_SELECT_FEATURE_UNHIGHLIGHTED);
		mapEventTypesToGisTypes.put(
				EventType.CONTROL_SELECT_FEATURE_UNHIGHLIGHTED,
				CONTROL_SELECT_FEATURE_UNHIGHLIGHTED);
		mapGisTypesToEventTypes.put(LAYER_LOADSTART, EventType.LAYER_LOADSTART);
		mapEventTypesToGisTypes.put(EventType.LAYER_LOADSTART, LAYER_LOADSTART);
		mapGisTypesToEventTypes.put(LAYER_LOADEND, EventType.LAYER_LOADEND);
		mapEventTypesToGisTypes.put(EventType.LAYER_LOADEND, LAYER_LOADEND);
		mapGisTypesToEventTypes.put(LAYER_LOADCANCEL,
				EventType.LAYER_LOADCANCEL);
		mapEventTypesToGisTypes.put(EventType.LAYER_LOADCANCEL,
				LAYER_LOADCANCEL);
		mapGisTypesToEventTypes.put(LAYER_VISIBILITYCHANGED,
				EventType.LAYER_VISIBILITYCHANGED);
		mapEventTypesToGisTypes.put(EventType.LAYER_VISIBILITYCHANGED,
				LAYER_VISIBILITYCHANGED);
		mapGisTypesToEventTypes.put(MAP_BASE_LAYER_CHANGED,
				EventType.MAP_BASE_LAYER_CHANGED);
		mapEventTypesToGisTypes.put(EventType.MAP_BASE_LAYER_CHANGED,
				MAP_BASE_LAYER_CHANGED);
		mapGisTypesToEventTypes.put(MAP_CLICK, EventType.MAP_CLICK);
		mapEventTypesToGisTypes.put(EventType.MAP_CLICK, MAP_CLICK);
		mapGisTypesToEventTypes.put(MAP_LAYER_ADDED, EventType.MAP_LAYER_ADDED);
		mapEventTypesToGisTypes.put(EventType.MAP_LAYER_ADDED, MAP_LAYER_ADDED);
		mapGisTypesToEventTypes.put(MAP_LAYER_CHANGED,
				EventType.MAP_LAYER_CHANGED);
		mapEventTypesToGisTypes.put(EventType.MAP_LAYER_CHANGED,
				MAP_LAYER_CHANGED);
		mapGisTypesToEventTypes.put(MAP_LAYER_REMOVED,
				EventType.MAP_LAYER_REMOVED);
		mapEventTypesToGisTypes.put(EventType.MAP_LAYER_REMOVED,
				MAP_LAYER_REMOVED);
		mapGisTypesToEventTypes.put(MAP_MOVE, EventType.MAP_MOVE);
		mapEventTypesToGisTypes.put(EventType.MAP_MOVE, MAP_MOVE);
		mapGisTypesToEventTypes.put(MAP_MOVEEND, EventType.MAP_MOVEEND);
		mapEventTypesToGisTypes.put(EventType.MAP_MOVEEND, MAP_MOVEEND);
		mapGisTypesToEventTypes.put(MAP_ZOOMEND, EventType.MAP_ZOOMEND);
		mapEventTypesToGisTypes.put(EventType.MAP_ZOOMEND, MAP_ZOOMEND);
		mapGisTypesToEventTypes.put(MAP_POPUP_OPEN, EventType.MAP_POPUP_OPEN);
		mapEventTypesToGisTypes.put(EventType.MAP_POPUP_OPEN, MAP_POPUP_OPEN);
		mapGisTypesToEventTypes.put(MAP_POPUP_CLOSE, EventType.MAP_POPUP_CLOSE);
		mapEventTypesToGisTypes.put(EventType.MAP_POPUP_CLOSE, MAP_POPUP_CLOSE);
		mapGisTypesToEventTypes.put(MAP_MARKER_ADDED,
				EventType.MAP_MARKER_ADDED);
		mapEventTypesToGisTypes.put(EventType.MAP_MARKER_ADDED,
				MAP_MARKER_ADDED);
		mapGisTypesToEventTypes.put(MAP_MARKER_REMOVED,
				EventType.MAP_MARKER_REMOVED);
		mapEventTypesToGisTypes.put(EventType.MAP_MARKER_REMOVED,
				MAP_MARKER_REMOVED);
		mapGisTypesToEventTypes.put(VECTOR_BEFORE_FEATURE_ADDED,
				EventType.VECTOR_BEFORE_FEATURE_ADDED);
		mapEventTypesToGisTypes.put(EventType.VECTOR_BEFORE_FEATURE_ADDED,
				VECTOR_BEFORE_FEATURE_ADDED);
		mapGisTypesToEventTypes.put(VECTOR_FEATURE_ADDED,
				EventType.VECTOR_FEATURE_ADDED);
		mapEventTypesToGisTypes.put(EventType.VECTOR_FEATURE_ADDED,
				VECTOR_FEATURE_ADDED);
		mapGisTypesToEventTypes.put(VECTOR_FEATURE_MODIFIED,
				EventType.VECTOR_FEATURE_MODIFIED);
		mapEventTypesToGisTypes.put(EventType.VECTOR_FEATURE_MODIFIED,
				VECTOR_FEATURE_MODIFIED);
		mapGisTypesToEventTypes.put(VECTOR_FEATURE_REMOVED,
				EventType.VECTOR_FEATURE_REMOVED);
		mapEventTypesToGisTypes.put(EventType.VECTOR_FEATURE_REMOVED,
				VECTOR_FEATURE_REMOVED);
		mapGisTypesToEventTypes.put(VECTOR_FEATURE_SELECTED,
				EventType.VECTOR_FEATURE_SELECTED);
		mapEventTypesToGisTypes.put(EventType.VECTOR_FEATURE_SELECTED,
				VECTOR_FEATURE_SELECTED);
		mapGisTypesToEventTypes.put(VECTOR_FEATURE_UNSELECTED,
				EventType.VECTOR_FEATURE_UNSELECTED);
		mapEventTypesToGisTypes.put(EventType.VECTOR_FEATURE_UNSELECTED,
				VECTOR_FEATURE_UNSELECTED);

		mapGisTypesToEventTypes.put(MAP_MOUSEUP, "mouseup");
		mapEventTypesToGisTypes.put("mouseup", MAP_MOUSEUP);
	}
}
